<?php
// +----------------------------------------------------------------------
// | ManniuRead
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2016 http://manniuyun.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: heitao <mall@manniuyun.com>
// +----------------------------------------------------------------------

namespace app\mall\controller\admin;

use app\mall\base\MallAdminBase;

use app\mall\model\MallSpecCats as MallSpecCatsModel;
use app\mall\model\MallSpecGroups as MallSpecGroupsModel;
use app\mall\model\MallSpecItems as MallSpecItemsModel;

class SpecCats extends MallAdminBase{
    //当前控制器菜单
    public $submenu = [];

    /**
     * TP初始化方法
     */
    public function _initialize() {
        parent::_initialize();
        $this->submenu[] = ['title' => '规格分类列表', 'url' => url('mall/admin.specCats/index'), 'icon' => '&#xe60a;'];
        $this->submenu[] = ['title' => '规格组列表', 'url' => url('mall/admin.specGroups/index'), 'icon' => '&#xe60a;'];
        $this->submenu[] = ['title' => '规格列表', 'url' => url('mall/admin.specItems/index'), 'icon' => '&#xe60a;'];
        $this->submenu[] = ['title' => '添加规格分类', 'url' => url('mall/admin.specCats/add'), 'icon' => '&#xe608;'];
        $this->submenu[] = ['title' => '更新规格缓存', 'url' => url('mall/admin.specCats/cache'), 'icon' => 'ဂ'];
    }

    /**
     * 后台商品规格分类列表
     * @access public
     * @return \think\Response
     */
    public function index(){
        $list = MallSpecCatsModel::order('id desc')->paginate(config('mn_admin.admin_pagesize'));
        $this->assign([
            'submenu' => $this->submenu,
            'list' => $list
        ]);
    	return $this->fetch();
    }

    /**
     * 后台商品规格分类添加
     * @access public
     * @return \think\Response
     */
    public function add(){
        // POST提交数据
        if($this->request->isPost()){
            $post_data = $this->buildParam();
            // 实例化商品规格分类模型
            $MallSpecCatsModel = new MallSpecCatsModel;
            // 验证商品规格分类是否重复
            $check_repeat = MallSpecCatsModel::where('name',$post_data['name'])->count();
            if($check_repeat){
                $this->error('商品规格分类名称重复');
            }
            // 新增操作
            if($MallSpecCatsModel->allowField(true)->validate(true)->save($post_data)){
                // 更新所有规格缓存
                $MallSpecCatsModel->cache();
                $this->success('商品规格分类添加成功','index');
            }
            else{
                $this->error($MallSpecCatsModel->getError());
            }
        }
        $this->assign([
            'submenu' => $this->submenu
        ]);
        return $this->fetch();
    }

    /**
     * 后台商品规格分类修改
     * @access public
     * @return \think\Response
     */
    public function edit($id = 0){
        $id = (int)$id;
        if(!$id){
            $this->error('缺少商品规格分类id');
        }
        // 实例化商品规格分类模型
        $spec_cat = MallSpecCatsModel::get($id);
        if(!$spec_cat){
            $this->error('商品规格分类不存在','index');
        }
        // POST提交数据
        if($this->request->isPost()){
            $post_data = $this->buildParam();
            // 验证商品规格分类是否重复
            $check_repeat = MallSpecCatsModel::where('id','neq',$id)->where('name',$post_data['name'])->count();
            if($check_repeat){
                $this->error('商品规格分类名称重复');
            }
            // 更新操作
            if(false !== $spec_cat->allowField(true)->validate(true)->save($post_data,['id' => $id])){
                // 更新所有规格缓存
                $spec_cat->cache();
                $this->success('商品规格分类修改成功','index');
            }
            else{
                $this->error($spec_cat->getError());
            }
        }
        $this->assign([
            'submenu' => $this->submenu,
            'data'      => $spec_cat
        ]);
        return $this->fetch();
    }

    /**
     * 后台商品规格分类删除
     * @access public
     * @return void
     */
    public function del($id = 0){
        $id = (int)$id;
        if(!$id){
            $this->error('缺少商品规格分类id');
        }
        $spec_cat = MallSpecCatsModel::get($id);
        if(!$spec_cat){
            $this->error('需要删除的商品规格分类不存在');
        }
        // 删除规格分类下的所有规格组
        MallSpecGroupsModel::destroy(['spec_catid' => $id]);
        // 删除规格分类下的所有规格
        MallSpecItemsModel::destroy(['spec_catid' => $id]);
        // 删除规格分类
        $spec_cat->delete();
        // 更新所有规格缓存
        $spec_cat->cache();
        $this->success('商品规格分类删除成功', 'index');
    }

    /**
     * 更新缓存
     * @access public
     * @return void
     */
    public function cache(){
        // 实例化规格分类模型
        $MallSpecCatsModel = new MallSpecCatsModel;
        $MallSpecCatsModel->cache();
        $this->success('更新缓存成功！','index');
    }
}